package com.xpec.c4.net;

import java.net.InetSocketAddress;

import com.xpec.c4.service.module.gateway.GatewayModule;


abstract public class XSENetServer 
{
	protected InetSocketAddress mAddress;
	
	/**
	 * module name
	 */
	protected String name;

	/**
	 * 
	 */
	protected GatewayModule gatewayModule;
	

	public static final XSENetServer createServer(int libType, int protocolType)	{
		//目前implement僅支持TCP
		if( protocolType != XSENetConstant.PROTOCOLTYPE_TCP )
			return null;
		
		if( libType == XSENetConstant.LIB_CINDYMODE )
			return null; //new XSENetCindyServer(protocolType);
		else if( libType == XSENetConstant.LIB_NETTYMODE )
			return new XSENetNettyServer(protocolType);
		
		return null;
	}
	
	public void bind(int port)
	{
		mAddress = new InetSocketAddress(port);
	}
	
	public void bind(String ip, int port)
	{
		mAddress = new InetSocketAddress(ip, port);
	}
	
	abstract public void setHandleMode(int mode);
	
	abstract public void start();
	
	abstract public void close();
	
	abstract public boolean isStarted();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public GatewayModule getGatewayModule() {
		return gatewayModule;
	}

	public void setGatewayModule(GatewayModule gatewayModule) {
		this.gatewayModule = gatewayModule;
	}
	

	/**
	 * when shutting down, no new connection allowed
	 */
	private volatile boolean allowNewConnection = true;
	
	public boolean isNewConnectionAllowed() {
		return allowNewConnection;
	}

	public void allowNewConnection(boolean allowed) {
		allowNewConnection = allowed;
	}
	
	abstract public IMeter getMeter();	
}
